//
//  ZongJWorkViewController.m
//  Estate
//
//  Created by heyong on 16/5/26.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "ZongJWorkViewController.h"

@interface ZongJWorkViewController ()<UIScrollViewDelegate>

// 待处理数据
@property (nonatomic, strong) NSMutableArray *dataArray;

@end


@implementation ZongJWorkViewController

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


-(UIScrollView *)contentScrollview{
    
    if(_contentScrollview==nil){
        _contentScrollview=[[UIScrollView alloc]init];
    }
    return _contentScrollview;
    
}

- (UISegmentedControl *)headerSegment {
    if (_headerSegment==nil) {
        NSArray *arr = [[NSArray alloc]initWithObjects:@"安全", @"日志", @"日记", @"巡视", @"记录", nil];
        _headerSegment=[[UISegmentedControl alloc]initWithItems:arr];
        _headerSegment.frame=CGRectMake(10, 74, DEF_SCREEN_WIDTH-20, 35*BIZ);
    }
    return _headerSegment;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    //添加滚动视图
    [self.view addSubview:self.contentScrollview];
    //添加选择器
    [self.view addSubview:self.headerSegment];
    //创建视图样式
    [self createView];
    
    // 获取未处理数据
//    [self getPendingMattersZJ];
//    
//    if (self.dataArray.count > 0) {
//        // 设置小红点
//        self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", self.dataArray.count];;
//    }
}

// 获取待处理数量(总监工作)
- (void)getPendingMattersZJ {
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    [self.dataArray removeAllObjects];
    
    // 日记
    NSString *sqlRJ=[NSString stringWithFormat:@"select *,substr(DIARY_TIME,1,10) AS Day from WORK_DIARY WHERE PRO_ID = '%@' AND DIARY_USER != '%@' AND REVIEW_USER = '%@' AND REVIEW_STATE = '0' ORDER BY DIARY_TIME desc", PROID, USERID, USERID];
    FMResultSet *resultRJ = [db executeQuery:sqlRJ];
    
    while ([resultRJ next]) {
        //id
        NSString *ids=[resultRJ stringForColumn:@"ID"];
        
        if (ids && ![ids isEqualToString:@""]) {
            //添加到数组
            [self.dataArray addObject:ids];
        }
    }
    
    // 日志
    NSString *sqlRZ=[NSString stringWithFormat:@"SELECT substr(CREATE_TIME,1,10) AS Day,* FROM WORK_LOG WHERE PRO_ID = '%@' AND LOG_USERS != '%@' AND REVIEW_STATE = '0' ORDER BY CREATE_TIME desc ", PROID, USERID];
    
    FMResultSet *resultRZ = [db executeQuery:sqlRZ];
    while ([resultRZ next]) {
        //id
        NSString *ids=[resultRZ stringForColumn:@"ID"];
        
        if (ids && ![ids isEqualToString:@""]) {
            //添加到数组
            [self.dataArray addObject:ids];
        }
    }
    [db close];
}


#pragma mark -创建视图
- (void) createView{
    self.title=@"智慧监理";
    self.view.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    [self.contentScrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerSegment.mas_bottom).offset(10);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.offset(667*BIZ);
    }];
    //默认选中
    self.headerSegment.selectedSegmentIndex=0;
    //设置选择器样式
    [_headerSegment setTintColor:[TTTheme defaultManger].defaultColor];
    // 未选中颜色
    [_headerSegment setTitleTextAttributes:@{NSForegroundColorAttributeName:[TTTheme defaultManger].selectColor,NSFontAttributeName:[UIFont systemFontOfSize:17.*BIZ]} forState:UIControlStateNormal];
    //选中颜色 字体大小
    [_headerSegment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:17.*BIZ]} forState:UIControlStateSelected];
    //设置滚动视图
    [self setUpScrollView];
    //设置子视图
    [self setUpChildViewControll];
    //为选择器添加事件
    [_headerSegment addTarget:self action:@selector(segmentSelect:) forControlEvents:UIControlEventValueChanged];
    
}

//点击选择器事件
-(void)segmentSelect:(UISegmentedControl*)seg{
    NSInteger index = seg.selectedSegmentIndex;
    CGRect frame = _contentScrollview.frame;
    frame.origin.x = index * CGRectGetWidth(_contentScrollview.frame);
    frame.origin.y = 0;
    [_contentScrollview scrollRectToVisible:frame animated:YES];
}

//设置滚动视图属性
-(void)setUpScrollView{
    _contentScrollview.pagingEnabled = YES;
    _contentScrollview.bounces=NO;
    _contentScrollview.delegate = self;
    _contentScrollview.showsHorizontalScrollIndicator = NO;
    //方向锁
    _contentScrollview.directionalLockEnabled = YES;
    //取消自动布局
    self.automaticallyAdjustsScrollViewInsets = NO;
    _contentScrollview.contentSize = CGSizeMake(DEF_SCREEN_WIDTH * 5, DEF_SCREEN_HEIGHT);
}

/**
 *  设置控制的每一个子控制器
 */
-(void)setUpChildViewControll{
    //初始化视图控制器
    self.safeVC = [[SafeListViewController alloc] init];
    
    self.zjrzCtr = [[ZJRZViewController alloc]init];
    self.zjrjCtr=[[ZJRJViewController alloc]init];
    self.patrolVC = [[PatrolListViewController alloc] init];
    self.zjjlCtr = [[ZJJLViewController alloc]init];

    
    //指定该控制器为其子控制器
    [self addChildViewController:_safeVC];
    [self addChildViewController:_zjrzCtr];
    [self addChildViewController:_zjrjCtr];
    [self addChildViewController:_patrolVC];
    [self addChildViewController:_zjjlCtr];

    //将视图view加入到scrollview上
    [_contentScrollview addSubview:_safeVC.view];
    [_contentScrollview addSubview:_zjrzCtr.view];
    [_contentScrollview addSubview:_zjrjCtr.view];
    [_contentScrollview addSubview:_patrolVC.view];
    [_contentScrollview addSubview:_zjjlCtr.view];
    
    //!!!设置控制器的位置
    //安全
    CGRect safeRect = _safeVC.view.frame;
    safeRect.origin.x = 0;
    safeRect.size.height =CGRectGetHeight(_contentScrollview.frame);
    _zjrzCtr.view.frame = safeRect;
    
    //日志
    CGRect rzCtrRect = _zjrzCtr.view.frame;
    rzCtrRect.origin.x = DEF_SCREEN_WIDTH;
    rzCtrRect.size.height =CGRectGetHeight(_contentScrollview.frame);
    _zjrzCtr.view.frame = rzCtrRect;
    
    //日记
    CGRect rjCtrRect = _zjrjCtr.view.frame;
    rjCtrRect.origin.x = DEF_SCREEN_WIDTH*2;
    rjCtrRect.size.height = CGRectGetHeight(_contentScrollview.frame);
    _zjrjCtr.view.frame = rjCtrRect;
    
    //巡视
    CGRect patrolRect = _patrolVC.view.frame;
    patrolRect.origin.x = 3*DEF_SCREEN_WIDTH;
    patrolRect.size.height = CGRectGetHeight(_contentScrollview.frame);
    _patrolVC.view.frame = patrolRect;
    
    //记录
    CGRect jlCtrRect = _zjjlCtr.view.frame;
    jlCtrRect.origin.x = 4*DEF_SCREEN_WIDTH;
    jlCtrRect.size.height = CGRectGetHeight(_contentScrollview.frame);
    _zjjlCtr.view.frame = jlCtrRect;
}

#pragma mark -UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    [UIView animateWithDuration:0.4 animations:^{
        self.zjjlCtr.listView.frame =CGRectMake(DEF_SCREEN_WIDTH, 0,  DEF_SCREEN_WIDTH*0.85, DEF_SCREEN_HEIGHT);
    }];
    
    CGFloat offSetX = scrollView.contentOffset.x;
    NSInteger ratio = round(offSetX / DEF_SCREEN_WIDTH);
    _headerSegment.selectedSegmentIndex = ratio;

}


@end

